/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.com.impl.dao;

import e.z.blog.back.bean.State;
import e.z.blog.back.bean.base.Tag;
import e.z.blog.back.com.DaoTagApi;
import java.util.List;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 *
 * @author EnzoZhong
 */
@IocBean
public class DaoTag implements DaoTagApi {

        @Inject
        private Dao dao;

        @Override
        public Tag create ( Tag type ) {
                type.setState( State.YELLOW );
                return dao.insert( type );
        }

        @Override
        public Tag read ( Tag type ) {
                Integer tagId = type.getTagId();
                String tagName = type.getTagName();
                Cnd cnd = Cnd.where( "tagId" , "=" , tagId ).or( "tagName" , "=" , tagName );
                return dao.fetch( Tag.class , cnd );
        }

        @Override
        public List<Tag> readAll () {
                return dao.query( Tag.class , null , null );
        }

        @Override
        public Tag update ( Tag type ) {
                dao.update( type );
                return type;
        }

        @Override
        public Tag delete ( Tag type ) {

                type = read( type );
                switch ( type.getState() ) {
                        case State.BLACK: {
                                dao.clearLinks( type , "tagList" );
                                dao.delete( type );
                                break;
                        }
                        default: {
                                type.setState( type.getState() + 1 );
                                dao.update( type );
                                break;
                        }
                }

                return type;
        }
}
